/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制对象池管理器的窗体
* CreateTime：2020-07-07 17:38:59
* Version：1.0
* Modify Recorder：
*************************************************/

using System.Collections.Generic;
using TinyToolKit;
using TinyToolKit.ObjectPool;
using UnityEngine;
using UnityEditor;

namespace TinyToolKitEditor 
{
    public class DrawWindowObjectPool : DrawWindowBase
    {
        private TObjectPoolManager _objectManager;

        protected override void DrawInternalMainGUI()
        {
            TinyToolWindowUtil.DrawDefaultEditorGUI(); 
            
            if (EditorApplication.isPlaying == false || TinyTool.Instance.HasManager<TObjectPoolManager>() == false) return;

            if (_objectManager == null) _objectManager = TinyTool.Instance.GetManager<TObjectPoolManager>();

            
            Dictionary<string, TObjectPool<GameObject>> allPrefabMaps = _objectManager.PrefabMaps;
            if (allPrefabMaps != null && allPrefabMaps.Count > 0)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                TinyToolWindowUtil.DrawTableHead("名称", "状态");
                foreach (KeyValuePair<string, TObjectPool<GameObject>> objectPool in allPrefabMaps)
                {
                    string fsmName = objectPool.Key;
                    string capacity = objectPool.Value.Capacity.ToString();
                    string usingCount = objectPool.Value.usingCount.ToString();
                    string unusedCount = objectPool.Value.unUsedCount.ToString();
                    string isAutoRelease = false.ToString();
                    string status;
                    if (objectPool.Value.IsAutoRelease)
                    {
                        string autoTime = objectPool.Value.AutoReleaseTime.ToString();
                        string curTime = objectPool.Value.CurReleaseTimer.ToString("F1");

                        status = $"容量：{capacity}，使用个数：{usingCount}，未使用个数：{unusedCount}，自动释放：{isAutoRelease}，释放间隔：{autoTime}，当前时间：{curTime}";
                    }
                    else
                    {
                        status = $"容量：{capacity}，使用个数：{usingCount}，未使用个数：{unusedCount}，自动释放：{isAutoRelease}";
                    }
                    EditorGUILayout.LabelField(fsmName, status);
                }
                TinyToolWindowUtil.DrawTableTail("总计：", _objectManager.Count.ToString());
                EditorGUILayout.EndVertical();
            }
            
        }
    }
}
